<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib prefix="sys" uri="/WEB-INF/tld/sys.tld" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>借款申请</title>
    <style type="text/css">
    </style>
    <jsp:include page="../../../../import.jsp" flush="true"/>
</head>

<body class="iframeBody">
<div class="Wrapper">

    <div class="sqPanel" style="margin-bottom: 5px;">
        <span onclick="toAddPage()" id="addBtn" class="sq-button sq-button-round" style="margin:5px">新增</span>
        <span onclick="deleteRow()" id="removeBtn" class="sq-button sq-button-round" style="margin:5px">删除</span>

        <div class="sqPanel_head" >

            <table class="table_full">
                <tr>
                    <td><b style="color:red">绑定资金【本次申请付款金额：${payAmt}; 费用项： ${loanTypCdNm}】 </b></td>
                    <td><b style="color:red">注意：如不维护使用金额，本次申请付款金额将均摊至已选资产，请谨慎操作</b></td>
                    <td id="leftMoneyTd"><b style="color:red">剩余未使用金额:<span id="leftMoney">0</span></b></td>
                </tr>
            </table>
        </div>
            <div class="sqPanel_body">
                <div id="ligergrid1" class="sq-grid"></div>
            </div>
            <div class="sqPanel_head" style="height:35px;">
                <table class="table_full">
                    <tr>
                        <td>
                            <span onclick="doSave()" id="doSaveBtn"class="sq-button sq-button-round" style="margin:5px">保存绑定资产</span>
                        </td>
                    </tr>
                </table>
            </div>
    </div>

    <!-- 付款信息 -->
</div>



<script type="text/javascript">
    var grid = null, form = null;
    var payDtlId = '${payDtlId}';
    var editAble='${editAble}';
    $(function () {
        setEditAble(editAble);
        var option = {
            url:'<%=path%>/capital/finance/loan/apply/asset/initBindList.do?payDtlId='+payDtlId,
            method:'get',
            columns: [
                {display: 'id', name: 'id', minWidth: 100, hide: true},
                {display: '支付表号', name: 'paySchId', hide:true},
                {display: '承租人', name: 'cstNm', minWidth: 100, align: 'left', enabledSort: false},
                {display: '合同号', name: 'contNo', minWidth: 130, align: 'left', enabledSort: false},
                {display: '支付表号', name: 'paySchNo', minWidth: 130, align: 'left', enabledSort: false},
                {display: '所属事业部', name: 'buOrgCdNm', minWidth: 100, align: 'left', enabledSort: false},
                {display: '所属省公司', name: 'prOrgCdNm', minWidth: 100, align: 'left', enabledSort: false},
                {display: '所属分公司', name: 'cyOrgCdNm', minWidth: 100, align: 'left', enabledSort: false},
                {display: '客户经理', name: 'cstMgrNm', minWidth: 100, align: 'left', enabledSort: false},
                /*{display: '所属区域', name: 'cstMgrRgnCdNm', minWidth: 100, align: 'left', enabledSort: false},*/
                {display: '使用金额（元）', name: 'payAmt', minWidth: 100, align: 'left', enabledSort: false,editor:{type:'number'}},
                {
                    display: '备注', name: 'rem', width: 100, align: 'right', enabledSort: false,editor:{type:'text'}
                },
            ],
            enabledEdit: true, checkbox:true,rownumbers:true,isScroll: false,width: '100%',root:'rows',onAfterEdit:calLeftMoney

        };
        if(editAble == "0"){
            option.enabledEdit = false;
        }
        grid = $("#ligergrid1").ligerGrid(option);
        grid.setHeight(300);
        $("#pageloading").hide();
        calLeftMoney();
    });
    function setEditAble(editAble){
        if(editAble=="0"){ //不可编辑
            $("#doSaveBtn").hide();
            $("#addBtn").hide();
            $("#removeBtn").hide();
            $("#leftMoneyTd").hide();
        }
    }
    function doSave(){
        var data = grid.getData();
        for(var x of data){
            x.payAmt = x.payAmt * 100;
        }
        // ajax
        var url = "<%=path%>/capital/finance/loan/apply/asset/doSave.do";
        var param = {data:JSON.stringify(data),payDtlId:payDtlId,payAmt:'${payAmt}'};
        $.post(url,param,function(res){
            var wcallback = GET_W2Wcallback();
            res = JSON.parse(res);
            if(res.success==1){
                UIUtils.alertSuccess(res.info)
                wcallback(data.length,payDtlId);
                Remove_navTab();
            }else{
                UIUtils.alertError(res.info)
            }
        })
    }
    /**
     *  添加资产 回调函数
     */
    function toAddPageCallBack(rows){
        for(var row of rows){
            row.payAmt=0;
            var flag = containsRow(row);
            if(flag==false){
                grid.addRow(row)
            }
        }
    }
    function containsRow(row){
        var data = grid.getData();
        for(var a of data){
            if(row.paySchId==a.paySchId){
                return true;
            }
        }
        return false;
    }
    /**
     * 添加资产
     */
    function toAddPage(){
        parent.Add_navTab('tab_fiance_asset_list'+payDtlId,'选择资产','<%=path%>/capital/finance/loan/apply/asset/toListPage.do',toAddPageCallBack);
    }

    /**
     * 初始化剩余金额数值
     */
    var finPayDtlRListSize = ${finPayDtlRListSize};
    function initLeftMoney() {
        var leftMoney = 0 ;
        if(finPayDtlRListSize ==0){ // 没有绑定
            leftMoney = ${payAmt};
        }else{
            //已经绑定肯定是0 因为剩余金额会自动分配
        }
        $("#leftMoney").html(leftMoney);
    }
    /**
     * 每次编辑结束后  重新计算剩余金额
     */
    function calLeftMoney(){
        var leftMoney = ${payAmt};
        var data = grid.getData();
        if(data && data.length >0 ){
            for (var row of data){
                leftMoney = leftMoney-row.payAmt;
            }
        }
        $("#leftMoney").html(leftMoney);
    }
    function deleteRow()
    {
        if (!confirm("确认要删除吗？")) {
            return false;
        }

        var rows = grid.getSelectedRows();
        if (rows) {
            var idArray = new Array();
            for (var row of rows) {
                if (row.id) {
                    idArray.push(row.id);
                }
            }
            var url = "<%=path%>/capital/finance/loan/apply/asset/delete.do"
            $.get(url, {data: JSON.stringify(idArray)}, function () {

            })
        }
        grid.deleteSelectedRow();
        setTimeout(calLeftMoney, 20 * rows.length);
    }
    function addNewRow()
    {
        var row = grid.getSelectedRow();
        //参数1:rowdata(非必填)
        //参数2:插入的位置 Row Data
        //参数3:之前或者之后(非必填)
        grid.addRow();
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }
</script>

</body>
</html>
